/*
 * Copyright (c) 2010 MoSync AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <ma.h>
#include <MAInterpreter/LuaInterpreter.h>
#include <MAInterpreter/InterpreterMoblet.h>
#include <conprint.h>
#include "MAHeaders.h"

using namespace MAInterpreter;

// Lua code is written directly in the C source file using defines.
// Note that there are no line breaks in the following code snippets. This can
// potentially cause confusion, e.g. if inserting a Lua comment -- the rest of
// the code will be commented out, not only the line as it appears in the
// define statement. Insert newlines \n when needed.

// Very simple painting program that draws rectangles on touch events.
#define PAINTING_PROGAM " \
maSetColor(0xffff00)\n\
maFillRect(0, 0, 500, 500) \
--DrawText('Lua <3', 20, 20, 'Arial32Black')\n \
maUpdateScreen() \
\
function Paint(x, y) \
  maSetColor(0x000000) \
  maFillRect(x - 10, y - 10, 20, 20) \
  maUpdateScreen() \
end \
\
System.onTouchDown(function(x, y) \
  Paint(x, y) \
end) \
\
System.onTouchDrag(function(x, y) \
  Paint(x, y) \
end) \
"
// The painting program using closures.
#define PAINTING_PROGAM2 " \
function Main() \
  maSetColor(0xFFFFFF) \
  maFillRect(0, 0, Screen.getWidth(), Screen.getHeight()) \
  maUpdateScreen() \
  \
  local function Paint(x, y, touchId) \
    maSetColor(0xFF * touchId) \
    maFillRect(x - 10, y - 10, 20, 20) \
    maUpdateScreen() \
  end \
\
  System.onTouchDown(Paint) \
  System.onTouchDrag(Paint) \
end \
\
Main() \
"
// Demo of how to use the timer.
#define TIMER_PROGAM " \
\
function Main() \
  WiperY = 0; \
  maSetColor(0x448822) \
  maFillRect(0, 0, 500, 500) \
  maUpdateScreen() \
  Timer1 = System.addTimer(100, function() \
    OnClockTick() \
  end) \
  Timer2 = System.addTimer(1000, function() \
    DrawBackground() \
  end) \
end \
\
System.onTouchDown(function() \
  --System.removeTimer(Timer1)\n \
  System.removeTimer(Timer2)\n \
  Timer2 = nil \
end) \
\
function OnClockTick() \
  WiperY = WiperY + 2; \
  if WiperY > 300 then \
    WiperY = 0; \
  end \
  DrawWiper() \
end \
\
function DrawWiper() \
  maSetColor(0xFFFFFF) \
  maFillRect(0, WiperY, 300, 2) \
  maUpdateScreen() \
end \
\
function DrawBackground() \
  maSetColor(0x448822) \
  maFillRect(0, 0, 500, 500) \
  maUpdateScreen() \
end \
\
Main() \
"

// Demo of how to open a textbox.
#define TEXTBOX_PROGRAM " \
\
maSetColor(0x448822) \
maFillRect(0, 0, 500, 500) \
maUpdateScreen() \
\
System.onKeyDown(function(key) \
  System.openTextBox( \
    'TextBox Test', \
    'KeyDown key: '..key, \
    function (result) \
      if nil == result then \
        print('TextBox was canceled') \
      else \
        print('TextBox result: '..result) \
      end \
    end) \
end) \
"

#define DEBUGTEST "maSetColor(0xFF0000)\nmaFillRect(0,0,200,200)\nmaUpdateScreen()\n"

int PaintAppInLua()
{
	LuaInterpreter* interpreter = new LuaInterpreter();
	if (!interpreter->initialize()) { return -1; }

	InterpreterMoblet* moblet = new InterpreterMoblet(interpreter);
	//interpreter->eval(DEBUGTEST);
	//interpreter->eval(PAINTING_PROGAM2);
	interpreter->eval(TIMER_PROGAM);
	//interpreter->eval(TEXTBOX_PROGRAM);
	//interpreter->eval("SystemLog('hej' .. 3)");

	moblet->mainLoop();

	delete moblet;

	return 0;
}
